package com.memory.monitor.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.memory.monitor.common.DeleteRequest;
import com.memory.monitor.common.ErrorCode;
import com.memory.monitor.constant.ExcelConstant;
import com.memory.monitor.exception.BusinessException;
import com.memory.monitor.exception.ThrowUtils;
import com.memory.monitor.job.RoleExcelListener;
import com.memory.monitor.job.StationExcelListener;
import com.memory.monitor.mapper.StationMapper;
import com.memory.monitor.model.dto.station.StationAddRequest;
import com.memory.monitor.model.dto.station.StationQuerryRequest;
import com.memory.monitor.model.dto.station.StationUpdateRequest;
import com.memory.monitor.model.entity.Role;
import com.memory.monitor.model.entity.Station;
import com.memory.monitor.model.entity.User;
import com.memory.monitor.model.vo.RoleVO;
import com.memory.monitor.model.vo.StationVO;
import com.memory.monitor.service.StationService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.memory.monitor.constant.ExcelConstant.*;

/**
* @author 21731
* @description 针对表【station(基地风采)】的数据库操作Service实现
* @createDate 2024-09-02 09:55:33
*/
@Service
public class StationServiceImpl extends ServiceImpl<StationMapper, Station>
    implements StationService {

    @Override
    public StationVO getStationVO(Station station) {
        if (station == null) {
            return null;
        }
        StationVO stationVO = new StationVO();
        BeanUtils.copyProperties(station, stationVO);
        return stationVO;
    }

    @Override
    public List<StationVO> getStationVO(List<Station> stationList) {
        if (CollectionUtils.isEmpty(stationList)) {
            return new ArrayList<>();
        }
        return stationList.stream().map(this::getStationVO).collect(Collectors.toList());
    }

    @Override
    public QueryWrapper<Station> getQueryWrapper(StationQuerryRequest stationQuerryRequest) {
        if (stationQuerryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Long id = stationQuerryRequest.getId();
        String stationTitle = stationQuerryRequest.getStationTitle();
        String stationPhoto = stationQuerryRequest.getStationPhoto();
        String stationDescription = stationQuerryRequest.getStationDescription();



        QueryWrapper<Station> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(id != null, "id", id);
        queryWrapper.like(StringUtils.isNotBlank(stationTitle), "station_title", stationTitle);
        queryWrapper.like(StringUtils.isNotBlank(stationPhoto), "station_photo", stationPhoto);
        queryWrapper.like(StringUtils.isNotBlank(stationDescription), "station_description", stationDescription);
        return queryWrapper;
    }

    @Override
    public Long addStation(StationAddRequest stationAddRequest) {
        if (stationAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Station station = new Station();

        BeanUtils.copyProperties(stationAddRequest, station);
        boolean result = this.save(station);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return station.getId();
    }

    @Override
    public Boolean updateStation(StationUpdateRequest stationUpdateRequest) {
        if (stationUpdateRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Station station = new Station();
        BeanUtils.copyProperties(stationUpdateRequest,station);
        boolean result = this.updateById(station);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    @Override
    public Boolean deleteStation(DeleteRequest deleteRequest) {
        if (deleteRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = this.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(result, ErrorCode.OPERATION_ERROR);
        return result;
    }

    @Override
    public List<StationVO> listStationByPage() {
        return getStationVO(this.list());
    }

    @Override
    public Boolean readExcel(MultipartFile multipartFile) {
        // 1.转换文件为字节流
        InputStream inputStream;
        try {
            inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        // 2.执行文件处理逻辑
        EasyExcel.read(inputStream, Station.class, new StationExcelListener()).sheet().doRead();
        // 3.入库
        boolean saveBatch = this.saveBatch(StationExcelListener.CACHED_DATA_LIST);

        ThrowUtils.throwIf(!saveBatch, ErrorCode.PARAMS_ERROR);
        return true;
    }

    @Override
    public Boolean writeExcel(String path) {
        // 1.定义文件路径
        String fullPath = DOWNLOAD_PATH + AREA_TABLE_NAME;
        // 2.检查文件是否存在
        ExcelConstant.isExist(fullPath);
        // 3.根据路径导出
        List<StationVO> stationList = this.listStationByPage();
        EasyExcel.write(fullPath, User.class).sheet(AREA_SHEET_NAME).doWrite(stationList);

        return true;
    }
}




